<template>
  <!-- 商品详情页面 -->
  <div class="bodyall">
    <div class="titlelie">
      <div>商品详情</div>
      <div class="caethome"><div @click="goCart">购物车</div><div @click="gohome">首页</div></div>
    </div>
    <div class="titleiles">
      <div>
        <img
          class="imgtest"
          :src="item.picture"
        />
      </div>
      <div>
        <div>{{item.Commodityname}}</div>
        <div>详情:XXXX</div>
        <div>价格￥:{{item.Price}}</div>
        <div class="btnall">
          数量<button @click="number--">-</button><button>{{number}}</button><button @click="number++">+</button>
        </div>
        <div class="btntit">
          <button @click="addcart">加入购物车</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref ,watch} from 'vue';
import {useRouter,useRoute} from 'vue-router'
import { onBeforeMount } from '@vue/runtime-core';
import {useStore} from 'vuex'
export default {
    setup(){
        const router=useRouter()
        const route=useRoute()
        const store=useStore()
        // 接收到的参数
        const item=ref()
        // 数量
        const number=ref(1)
        let gohome=()=>{
            router.go(-1)
        }
        let goCart=()=>{
          router.push('/Cart')
        }
        onBeforeMount(()=>{ 
            // item.value=router.currentRoute.value.query   //接收值第二种方法
            item.value=route.query
        })
        // 加入购物车
        let addcart=()=>{
          item.value.number=number.value
          item.value.ischeckeditem=true
          store.commit('addProduct',item.value)
          alert('加入购物车成功')
          router.push('/Cart')
        }
        // 监听数量
        watch(number,(newvalue)=>{
          if(newvalue<0){
            number.value=0
          }
        })
        return{
            gohome,
            item,
            addcart,
            number,
            goCart
        }
    }
};
</script>

<style scoped>
.bodyall {
  background: #dfdcdc;
  height: 100vh;
}
.titlelie {
  margin-left: 15%;
  padding-top: 30px;
  width: 70%;
  display: flex;
  justify-content: space-between;
}
.titlelie > :nth-child(1) {
  font-size: 24px;
  font-weight: 100;
}
.titlelie>:nth-child(2){
    cursor: pointer;
}
.titlelie>:nth-child(2)>:nth-child(2):hover{
    color: rgb(255, 0, 0);
}
.titlelie>:nth-child(2)>:nth-child(1):hover{
    color: rgb(255, 0, 0);
}
.imgtest {
  width: 400px;
  height: 400px;
}
.titleiles {
  margin-top: 30px;
  display: flex;
  justify-content: space-around;
  margin-left: 30%;
  width: 40%;
}
.titleiles > :nth-child(2) {
  margin-left: 10px;
}
.titleiles > :nth-child(2) > :nth-child(1) {
  background: white;
  width: 200px;
  height: 30px;
  padding-top: 10px;
  padding-left: 10px;
}
.titleiles > :nth-child(2) > :nth-child(2) {
  margin-top: 10px;
  background: white;
  width: 200px;
  height: 230px;
  padding-left: 10px;
}
.titleiles > :nth-child(2) > :nth-child(3) {
    padding-top: 10px;
  margin-top: 10px;
  background: white;
  width: 200px;
  height: 30px;
  padding-left: 10px;
}
.titleiles > :nth-child(2) > :nth-child(4) {
  margin-top: 10px;
}
.titleiles > :nth-child(2) > :nth-child(5) {
  margin-top: 10px;
}
.btnall > button {
  width: 30px;
  height: 20px;
  cursor: pointer;
  background: white;
  border: 0.25px rgb(180, 178, 178) solid;
}
.btntit > :nth-child(1) {
  width: 100px;
  height: 30px;
  background: #333333;
  color: white;
  border: none;
  font-size: 14px;
  font-weight: 100;
  line-height: 100%;
  text-align: center;
  border-radius: 4px;
  cursor: pointer;
}
.btntit > :nth-child(1) {
  width: 100px;
  height: 30px;
  background: white;
  color: #333333;
  border: none;
  font-size: 14px;
  font-weight: 100;
  line-height: 100%;
  text-align: center;
  border-radius: 4px;
  cursor: pointer;
}
.caethome{
  display: flex;
  justify-content: space-between;
  width: 80px;
}
</style>